package com.jk.lesson5;

import com.jk.bean.Dish;
import com.jk.lesson4.MenuTest;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Auser on 2016/11/11.
 */
public class UseStreamTest {

    private static List<Dish> menu = MenuTest.menu;

    public static void main(String...args){
        test_5_1_1();
        test_5_1_2();
        test_5_1_3();
        test_5_1_4();
        test_5_2_1();
        test_5_2_2();
        test_5_2_3();
    }

    /**
     * 5.1.1用谓词筛选
     */
    public static void test_5_1_1(){
        List<Dish> dishes = MenuTest.menu.stream()
                .filter(Dish::isVegetarian) // 素食
                .sorted(Comparator.comparing(Dish::getCalories).reversed())
                .collect(Collectors.toList());
        System.out.println(dishes.toString());
    }

    /**
     * 筛选各异元素
     */
    public static void test_5_1_2(){
        List<Integer> numbers = Arrays.asList(1,2,3,3,3,3,4,2,5,4,6);
        numbers.stream()
                .filter(integer -> integer % 2 == 0)
//                .distinct()
                .forEach(System.out::println);
    }

    /**
     * 截短流
     */
    public static void test_5_1_3(){
        List<Dish> dishes = MenuTest.menu.stream()
                .filter(dish -> dish.getCalories() > 300)
                .sorted(Comparator.comparing(Dish::getCalories).reversed())
                .limit(3)
                .collect(Collectors.toList());
        System.out.println(dishes.toString());
    }

    /**
     * 跳过元素
     */
    public static void test_5_1_4(){
        List<Dish> dishes = menu.stream()
                .filter(dish -> dish.getCalories() > 300)
                .sorted(Comparator.comparing(Dish::getCalories).reversed())
                .skip(3)
                .collect(Collectors.toList());
        System.out.println(dishes);
    }

    /**
     * 测验5.2：映射
     */
    public static void test_5_2_1(){
        List<Integer> numbers = Arrays.asList(1,2,3,4,5);
        List<Integer> squares = numbers.stream()
                .map(integer -> integer * integer)
                .collect(Collectors.toList());
        System.out.println(squares.toString());
    }

    public static void test_5_2_2(){
        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        List<String> pairs = numbers1.stream()
                .flatMap(integer1 -> numbers2.stream()
                        .map(integer2 -> "(" + integer1 + "," + integer2 + ")")
                )
//                .flatMap(Arrays::stream)
                .collect(Collectors.toList());
        System.out.println(pairs.toString());
    }

    public static void test_5_2_3(){
        List<Integer> numbers1 = Arrays.asList(1, 2, 3);
        List<Integer> numbers2 = Arrays.asList(3, 4);
        List<String> pairs = numbers1.stream()
                .flatMap(integer -> numbers2.stream()
                    .filter(integer1 -> (integer + integer1) % 3 == 0)
                    .map(integer1 -> "(" + integer + "," + integer1 + ")"))
                .collect(Collectors.toList());
        System.out.println(pairs.toString());

    }
}
